﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using HefaSyncTool.Properties;

namespace HefaSyncTool.Controls
{
    public partial class LoginAccountSet : UserControl
    {
        public delegate void CheckLoginHanlder(LoginAccountSet sender, string username, string password);
        public delegate void StopWorkHandler(LoginAccountSet sender,bool stopOrstart);
        public delegate void OpenRegisterHanlder(LoginAccountSet sender);

        public event CheckLoginHanlder CheckLogin;
        public event OpenRegisterHanlder OpenRegister;
        public event StopWorkHandler StopWork;

        private bool isSubSuccess = false;
        public LoginAccountSet()
        {
            InitializeComponent();
        }

        public LoginAccountSet(long webit,string username,string password,string icon, bool isSucc,bool isStart)
            :this()
        {
            WebID = webit;
            UserName = username;
            Password = password;
            WebIconFile = icon;
            Success = isSucc;
            IsStart = isStart;
        }

        public long WebID
        {
            get;
            set;
        }

        public Image WebIcon
        {
            set
            {
                this.pictureBox1.Image = value;
            }
        }

        public string WebIconFile
        {
            set
            {
                this.pictureBox1.Image = Image.FromFile(value);
            }
        }

        public bool Success
        {
            set
            {
                if (value)
                {
                    glassButton1.Image = Resources.Correct;
                }
                else
                {
                    glassButton1.Image = Resources.Wrong;
                }
                isSubSuccess = value;
            }
            get
            {
                return isSubSuccess;
            }
        }

        public bool IsStart
        {
            get
            {
                return this.kryptonCheckBox1.Checked;
            }
            set
            {
                this.kryptonCheckBox1.Checked = value;
            }
        }

        public string UserName
        {
            get
            {
                if (string.IsNullOrEmpty(this.textBox1.Text))
                {
                    return "";
                }
                else
                {
                    return this.textBox1.Text.Trim();
                }
            }
            set
            {
                this.textBox1.Text = value;
            }
        }

        public string Password
        {
            get
            {
                if (string.IsNullOrEmpty(this.textBox2.Text))
                {
                    return "";
                }
                else
                {
                    return this.textBox2.Text.Trim();
                }
            }
            set
            {
                this.textBox2.Text = value;
            }
        }

        public void ClearPassword()
        {
            this.textBox2.Text = "";
        }

        private void glassButton1_Click(object sender, EventArgs e)
        {
            if (CheckLogin != null)
            {
                if (string.IsNullOrEmpty(this.UserName))
                {
                    HFMessageBox.ShowAlert("提示","请输入帐号");
                    return;
                }
                if (string.IsNullOrEmpty(this.Password))
                {
                    HFMessageBox.ShowAlert("提示","请输入密码");
                    return;
                }
                CheckLogin(this, UserName, Password);
            }
        }

        private void glassButton2_Click(object sender, EventArgs e)
        {
            if (OpenRegister != null)
            {
                OpenRegister(this);
            }
        }

        private void RaiseStop()
        {
            if (StopWork != null)
            {
                StopWork(this, this.kryptonCheckBox1.Checked);
            }
        }

        private void kryptonCheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (StopWork != null)
            {
                StopWork(this, this.kryptonCheckBox1.Checked);
            }
        }
    }
}
